package com.aipanda.entity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * 
 * ---商品管理->规格管理
 * 
 * 产品规格类 例如：<br/>
 * 产品规格:颜色[女装,男装]，尺码[女装,男装]<br/>
 * 
 * @param name 					规格名称
 * @param type 					规格类型
 * @param memo 					备注
 * @param specificationValues 	规格值 [一对多关系]
 * @param products 				产品	  [多对多关系]
 * @author wanyuxiang
 * 
 */
@Entity
@Table(name = "aipanda_specification")
public class Specification extends OrderEntity {
	private static final long serialVersionUID = -6346775052811140926L;
	private String name;
	private Type type;
	private String memo;
	private List<SpecificationValue> specificationValues = new ArrayList<SpecificationValue>();
	private Set<Product> products = new HashSet<Product>();

	public enum Type {
		text, image;
	}

	@NotEmpty
	@Length(max = 255)
	@Column(nullable = false)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@NotNull
	@Column(nullable = false)
	public Type getType() {
		return this.type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	@Length(max = 255)
	public String getMemo() {
		return this.memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	// 在一对多关系里，关系的维护端都是在多的那一面，多的一面为主控方，拥有指向对方的外键。
	// FetchType.LAZY：懒加载，定义懒加载的属性不会马上从数据库中加载。
	// FetchType.EAGER：急加载，和懒加载相反

	@Valid
	@NotEmpty
	@OneToMany(mappedBy = "specification", cascade = { CascadeType.ALL }, orphanRemoval = true)
	@OrderBy("order asc")
	public List<SpecificationValue> getSpecificationValues() {
		return this.specificationValues;
	}

	public void setSpecificationValues(List<SpecificationValue> specificationValues) {
		this.specificationValues = specificationValues;
	}

	@ManyToMany(mappedBy = "specifications", fetch = FetchType.LAZY)
	public Set<Product> getProducts() {
		return this.products;
	}

	public void setProducts(Set<Product> products) {
		this.products = products;
	}
}
